package com.jhd.jiangxi.activity.scan.loss;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jhd.jiangxi.R;
import com.jhd.jiangxi.activity.main.BaseActivity;
import com.jhd.jiangxi.vo.PiatsInfo;
import com.jhd.jiangxi.vo.PiatsShow;
import com.jhd.jiangxi.vo.VaccineInfoScrap;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jhd.jiangxi.activity.scan.loss.ScanLossActivity.vaccineInfoScrapList;

public class LossDetailActivity extends BaseActivity {

    @BindView(R.id.btnBack)
    ImageButton btnBack;
    @BindView(R.id.tvHeaderUserName)
    TextView tvHeaderUserName;
    @BindView(R.id.rvStorage)
    RecyclerView rvStorage;
    @BindView(R.id.lin_number)
    LinearLayout lin_number;
    @BindView(R.id.tv_total)
    TextView tv_total;
    private DetailAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loss_detail);
        ButterKnife.bind(this);
        lin_number.setVisibility(View.VISIBLE);
        initHeader3("报损汇总明细");
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rvStorage.setLayoutManager(layoutManager);
        showDetail();

    }
    public void showDetail() {
        if (vaccineInfoScrapList.size() > 0) {
            int totalNum=0;
            for (VaccineInfoScrap vaccineInfoScrap : vaccineInfoScrapList) {
                PiatsShow piatsShow = new PiatsShow();
                piatsShow.setStorage(vaccineInfoScrap.getStorageCodeVoList().get(0).getStorageName());
                piatsShow.setVaccineId(vaccineInfoScrap.getVaccineId());
                piatsShow.setVaccineName(vaccineInfoScrap.getVaccineName());
                piatsShow.setLotNumber(vaccineInfoScrap.getLotNumber());
                if (!piatsShows.contains(piatsShow)) {
                    piatsShows.add(piatsShow);
                }
                totalNum = totalNum + Integer.valueOf(vaccineInfoScrap.getQuantity());
            }
            tv_total.setText(String.valueOf(totalNum));
            for (PiatsShow piatsShow : piatsShows) {
                int num = 0;
                for (VaccineInfoScrap vaccineInfoScrap : vaccineInfoScrapList) {
                    if (piatsShow.getStorage().equals(vaccineInfoScrap.getStorageCodeVoList().get(0).getStorageName()) && piatsShow.getVaccineId().equals(vaccineInfoScrap.getVaccineId())&&piatsShow.getLotNumber().equals(vaccineInfoScrap.getLotNumber())) {
                        num = num + Integer.valueOf(vaccineInfoScrap.getQuantity());
                    }
                }
                piatsShow.setNum(num);
            }

            adapter = new DetailAdapter(piatsShows);
            rvStorage.setAdapter(adapter);
        }
    }

    public class DetailAdapter extends RecyclerView.Adapter<DetailAdapter.ViewHolder> {
        private List<PiatsShow> piatsShows;

        public DetailAdapter(List<PiatsShow> piatsShows) {
            this.piatsShows = piatsShows;
        }

        @NonNull
        @Override
        public DetailAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_scan_detail_0, parent, false);
            DetailAdapter.ViewHolder viewHolder = new DetailAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull DetailAdapter.ViewHolder holder, int position) {
            holder.tvVaccineName.setText(piatsShows.get(position).getVaccineName());
            holder.tvScanNo.setText(String.valueOf(piatsShows.get(position).getNum()));
            holder.tvLotNumber.setText(String.valueOf(piatsShows.get(position).getLotNumber()));
            holder.tvStorage.setText(String.valueOf(piatsShows.get(position).getStorage()));
//
        }

        @Override
        public int getItemCount() {
            return piatsShows.size();
        }


        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvVaccineName, tvScanNo, tvLotNumber, tvStorage; //疫苗名称，扫码数

            public ViewHolder(View itemView) {
                super(itemView);
                this.tvVaccineName = itemView.findViewById(R.id.tvVaccineName);
                this.tvScanNo = itemView.findViewById(R.id.tvScanNo);
                this.tvStorage = itemView.findViewById(R.id.tvStorage);
                this.tvLotNumber = itemView.findViewById(R.id.tvLotNumber);
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        piatsShows.clear();
    }
}